package model.action;

import java.util.List;
import model.exception.ActionException;
import datacontainer.ImmuTable;


/**
 * 
 * @author Michael Deng
 * @author Jei Min Yoo
 * 
 */
public abstract class RowAction extends AbstractAction {

    private String myColumnName;
    private List<String> myConditions;

    @Override
    public void setInput (String column, List<String> conditions) {
        myColumnName = column;
        myConditions = conditions;
    }

    /**
     * subclasses can use myColumnName variable with this method.
     * 
     * @return myColumnName
     */
    protected String getColumnName () {
        return myColumnName;
    }

    /**
     * subclasses can use myConditions variable with this method.
     * 
     * @return myConditions
     */
    protected List<String> getConditions () {
        return myConditions;
    }

    /**
     * Method used only for Row comparisons
     * 
     * @param columnName
     * @param conditions
     * @param equal
     */
    protected void executeEqualityConditional (ImmuTable workSpace, String columnName,
                                               List<String> conditions, boolean deleteIfEqual) {
        int columnIndex = getWorkSpaceColumnIndex(workSpace, columnName);
        if (columnIndex != -1) {
            List<String> column = workSpace.getData().get(columnIndex);
            // i is an row index (counts down)
            for (int i = column.size() - 1; i >= 0; i--) {
                if (deleteIfEqual != isItEqual(column.get(i), conditions)) {
                    removeRow(workSpace, i);
                }
            }
        }
        else {
            throw new ActionException("Column does not exist in workspace");
        }
    }

    /**
     * Method used only for Row comparisons
     * 
     * @param element
     * @param conditions
     * @return
     */
    private boolean isItEqual (String element, List<String> conditions) {
        return conditions.contains(element);
    }
}
